/*
* ============================================================================
*  Name     : CMyEdwin from MyEdwin.h
*  Part of  : Mdnvbaox
*  Created  : 17.09.2007 by chenziteng@163.com
*  Implementation notes:
*     
*  Version  :
*  Copyright: 
* ============================================================================
*/

// INCLUDE FILES
#include <eikenv.h>
//#include <Utfjjwvx.mbg>
#include "MyEdwin.h"


// =================== CUSTOM DRAW =========================
class CMyCustomDraw : public CLafEdwinCustomDrawBase
    {
public:
	static CMyCustomDraw* NewL(const MLafEnv& aEnv,const CCoeControl& aControl);
    ~CMyCustomDraw();

public: // from MFormCustomDraw
	void DrawBackground(const TParam& aParam,const TRgb& aBackground,TRect& aDrawn) const;
	TRgb SystemColor(TUint aColorIndex,TRgb aDefaultColor) const;

public:
	void LineSpacingChanged();

private:
	CMyCustomDraw(const MLafEnv& aEnv,const CCoeControl& aControl);
    void ConstructL();

private:
    CFbsBitmap* iBitmap;
	};

CMyCustomDraw* CMyCustomDraw::NewL(const MLafEnv& aEnv,const CCoeControl& aControl)
    {
    CMyCustomDraw* self = new(ELeave) CMyCustomDraw(aEnv, aControl);
    CleanupStack::PushL(self);
    self->ConstructL();
    CleanupStack::Pop(self);
    return self;
    }

CMyCustomDraw::~CMyCustomDraw()
    {
    delete iBitmap;
    }

void CMyCustomDraw::DrawBackground(const TParam& aParam,const TRgb& aBackground,TRect& aDrawn) const
    {
    CLafEdwinCustomDrawBase::DrawBackground(aParam, aBackground, aDrawn);
    if((iBitmap!=NULL)&&(iBitmap->Handle()!=0))
        {
        TRect rect = TRect(TPoint(0,0), iBitmap->SizeInPixels());
        aParam.iGc.SetClippingRect(aParam.iDrawRect);
        aParam.iGc.DrawBitmap(rect, iBitmap);
        }
    else
        {
        aParam.iGc.SetBrushStyle(CGraphicsContext::ESolidBrush);
        aParam.iGc.SetBrushColor(TRgb(255, 251, 242));
        aParam.iGc.SetPenColor(TRgb(255, 251, 242));
        aParam.iGc.DrawRect(aDrawn);
        }
    }

TRgb CMyCustomDraw::SystemColor(TUint aColorIndex,TRgb aDefaultColor) const
    {
    return CLafEdwinCustomDrawBase::SystemColor(aColorIndex, aDefaultColor);
    }

void CMyCustomDraw::LineSpacingChanged()
    {
    CLafEdwinCustomDrawBase::LineSpacingChanged();
    }

CMyCustomDraw::CMyCustomDraw(const MLafEnv& aEnv,const CCoeControl& aControl)
 : CLafEdwinCustomDrawBase(aEnv, aControl)
    {
    }

void CMyCustomDraw::ConstructL()
    {
//#ifdef _DEBUG
//    _LIT(KMbmFile, "z:\\resource\\apps\\Utfjjwvx.mbm");
//#else
//    _LIT(KMbmFile, "c:\\resource\\apps\\Utfjjwvx.mbm");
//#endif
//    iBitmap = new(ELeave) CFbsBitmap;
//    TInt err = iBitmap->Load(KMbmFile, EMbmUtfjjwvxFlag);
//    User::LeaveIfError(err);
    }

// ================= MEMBER FUNCTIONS =======================
TKeyResponse CMyEdwin::OfferKeyEventL( 
                const TKeyEvent& aKeyEvent, 
                TEventCode aType )
{
    if (aType == EEventKey)
    {
        TInt lines = TextLayout()->NumFormattedLines();
        TInt curPos = CursorPos();
        TInt textLen = TextLength();
        TTmDocPosSpec docPos(curPos, TTmDocPosSpec::ELeftToRight);
        TTmLineInfo lineInfo;
        TTmPosInfo2 posInfo;
        
        TextView()->FindDocPosL(docPos, posInfo, &lineInfo);
        TInt index = lineInfo.iLineNumber;
        
        if (index <= 0 && aKeyEvent.iScanCode == EStdKeyUpArrow)
        {
            return EKeyWasNotConsumed;
        }
    if (index >= (lines - 1) && aKeyEvent.iScanCode == EStdKeyDownArrow)
        {
        return EKeyWasNotConsumed;
        }
    }
    
    if (aType == EEventKey)
        {
        if (aKeyEvent.iCode == EKeyDownArrow)
            {
            MoveCursorL (TCursorPosition::EFLineDown, EFalse);
            
            return EKeyWasConsumed;
            }
        else if (aKeyEvent.iCode == EKeyUpArrow)
            {
            MoveCursorL (TCursorPosition::EFLineUp, EFalse);
            
            return EKeyWasConsumed;
            }
        else
            {
            return CEikRichTextEditor::OfferKeyEventL(aKeyEvent, aType);
            }
        }

    return CEikRichTextEditor::OfferKeyEventL(aKeyEvent,aType);
}

CLafEdwinCustomDrawBase* CMyEdwin::CreateCustomDrawL()
    {
    return CMyCustomDraw::NewL(iEikonEnv->LafEnv(), *this);
    }
